<template>
	<view class="index_content">
		<swiperTabber :tabar="tabBars" :tabIndex="tabIndex" @tabtap="ontabtap" :tabbarItemStyle="{'width':'20%','text-align':'center'}"></swiperTabber>
		<swiper  class="swiper-box"  :style="{height:swiperHeight+'px'}" 
		:current="tabIndex" @change="tabchange">
			<swiper-item class="swiper-item" v-for="(tab,index1) in newsList" :key="index1">
				<scroll-view class="scroll-v list"  scroll-y @scrolltolower="loadMore(index1)">
					<template v-if="tab.list.length>0">
						<block v-for="(item,index) in tab.list" :key="index">
							<indexList :item="item"></indexList>
						</block>
						<!-- 上拉加载 -->
						<loadMore :loadtext="tab.loadtext"></loadMore>
					</template>
					<!-- <template v-else-if="">
						<view style="font-size: 50upx;color: #ccc;padding-top: 100upx;">Loding</view>
						
					</template> -->
					<template v-else>
						<!-- 默认无数据 -->
						<noThing></noThing>
					</template>
					
				</scroll-view>
			</swiper-item>
		</swiper>


	</view>
</template>

<script>
	import swiperTabber from '../../components/index/swiper-tabbar.vue';
	import indexList from '../../components/index/index-list.vue';
	import loadMore from '../../components/common/load-more.vue';
	import noThing from '../../components/common/no-thing.vue'
	export default {
		components: {
			indexList,
			swiperTabber,
			loadMore,
			noThing
		},
		data() {
			return {
				swiperHeight: 500,
				tabIndex: 0,
				tabBars: [],
				newsList: [{
						loadtext:'上拉加载更多',
						list: [{
								userpic: '../../static/img/userpic/1.jpg', //
								nickName: '小幸福', //昵称
								isFollow: true, //是否关注,
								title: '知识改变一切', //内容
								titlePic: '../../static/img/datapic/1.jpg',
								type: 'img', //图片或者视频
								info: {
									infoOperater: 1, //0没操作   1笑脸 2哭
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20w', //发布
								forward: '122' //转发
							},
							{
								userpic: '../../static/img/userpic/2.jpg', //头像
								nickName: '小幸福', //昵称
								isFollow: false, //是否关注,
								title: '知识改变一切', //内容
								type: 'video', //图片或者视频
								titlePic: '../../static/img/datapic/3.jpg',
								playnum: '20w',
								long: '10:44',
								info: {
									infoOperater: 0,
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20', //发布
								forward: '122' //转发
							}
						]
					},
					{
						loadtext:'上拉加载更多',
						list: [
							{
								userpic: '../../static/img/userpic/2.jpg', //头像
								nickName: '小幸福', //昵称
								isFollow: false, //是否关注,
								title: '知识改变一切', //内容
								type: 'video', //图片或者视频
								titlePic: '../../static/img/datapic/4.jpg',
								playnum: '20w',
								long: '10:44',
								info: {
									infoOperater: 0,
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20', //发布
								forward: '122' //转发
							},{
								userpic: '../../static/img/userpic/5.jpg', //头像
								nickName: '小幸福', //昵称
								isFollow: false, //是否关注,
								title: '知识改变一切', //内容
								type: 'video', //图片或者视频
								titlePic: '../../static/img/datapic/7.jpg',
								playnum: '20w',
								long: '10:44',
								info: {
									infoOperater: 0,
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20', //发布
								forward: '122' //转发
							},{
								userpic: '../../static/img/userpic/6.jpg', //头像
								nickName: '小幸福', //昵称
								isFollow: false, //是否关注,
								title: '知识改变一切', //内容
								type: 'video', //图片或者视频
								titlePic: '../../static/img/datapic/2.jpg',
								playnum: '20w',
								long: '10:44',
								info: {
									infoOperater: 0,
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20', //发布
								forward: '122' //转发
							}
						]
					},
					{
						loadtext:'上拉加载更多',
						list: [
							{
								headPortrait: '../../static/img/userpic/2.jpg', //头像
								nickName: '小幸福', //昵称
								isFollow: false, //是否关注,
								title: '知识改变一切', //内容
								type: 'video', //图片或者视频
								titlePic: '../../static/img/datapic/8.jpg',
								playnum: '20w',
								long: '10:44',
								info: {
									infoOperater: 0,
									laugh: 20, //笑哭
									cry: 10,
								},
								publish: '20', //发布
								forward: '122' //转发
							}
						]
					},
					{loadtext:'上拉加载更多',
						list: []
					},
					{loadtext:'上拉加载更多',
						list: []
					},
					{loadtext:'上拉加载更多',
						list: []
					},
				],
				list: [{
						headPortrait: '../../static/img/userpic/1.jpg', //头像
						nickName: '小幸福', //昵称
						isFollow: true, //是否关注,
						title: '知识改变一切', //内容
						titlePic: '../../static/img/datapic/1.jpg',
						type: 'img', //图片或者视频
						info: {
							infoOperater: 1, //0没操作   1笑脸 2哭
							laugh: 20, //笑哭
							cry: 10,
						},
						publish: '20w', //发布
						forward: '122' //转发
					},
					{
						headPortrait: '../../static/img/userpic/2.jpg', //头像
						nickName: '小幸福', //昵称
						isFollow: false, //是否关注,
						title: '知识改变一切', //内容
						type: 'video', //图片或者视频
						titlePic: '../../static/img/datapic/2.jpg',
						playnum: '20w',
						long: '10:44',
						info: {
							infoOperater: 0,
							laugh: 20, //笑哭
							cry: 10,
						},
						publish: '20', //发布
						forward: '122' //转发
					}
				]
			}
		},
		onLoad() {
			this.getNav()
			uni.getSystemInfo({
				success:(res)=> {
					console.log(res.windowHeight)
					console.log(res.screenHeight)
				let height=res.windowHeight-uni.upx2px(80)
					this.swiperHeight=height;
				}
			});
			
		},
		//监听原生标题看栏搜素输入框点击事件
		onNavigationBarSearchInputClicked(){
			uni.navigateTo({
				url: '/pages/search/search',
			});
		},
		//监听原生标题栏按钮点击事件
		onNavigationBarButtonTap(e){
			console.log(e)
			switch (e.index){
				case 0:
				uni.navigateTo({
					url: '../nav-echarts/nav-echarts',
				});
					break;
				default:
				uni.navigateTo({
					url: '../release/release',
				});
					break;
			}
		},
		methods: {
		    async getNav(){
				let [err,res] =await this.request.get('/postclass',{},{token:true,checkToken:false});
				
				if(err){
					return uni.showToast({
						title: '加载失败',icon:''
					});
				}
				let arr=[];
				let arrlist=[];
				res.data.data.list.forEach((item)=>{
					item.name=item.classname;
					arrlist.push({
						loadtext:'上拉加载更多',
						list:[],
						page:1,
						firstload:false
					})
				})
				this.tabBars=res.data.data.list;
				// this.newsList=arrlist;
				cosole.log(this.tabBars.length)
				if(this.tabBars.length>0) {this.getList()}
			},
			//获取指定列表
			async getList(){
				let url=`/postclass/${this.tabBars[this.tabIndex].id}/post/${this.newsList[this.tabIndex].page}`;
				let [err,res]=await this.request.get(url,{},{taken:true})
				cosole.log(res)
				// let list=res.data.data.list;
				// let arr=[];
				// cosole.log(list)
				// list.forEach((item)=>{
				// 	arr.push(this.format(item))
				// })
				// cosole.log(arr)
			},
			//格式转化
			format(item){
				return{
					userid:item.user.id,
					id:item.id,
					userpic: item.user.userpic,
					nickName: item.user.username, //昵称
					isFollow: false, //是否关注,
					title: item.title, //内容
					type: 'img', //图片或者视频
					titlePic: item.titlepic,
					playnum: '20w',
					long: '10:44',
					info: {
						infoOperater: (item.support.length>0) ? (item.support[0].type+1) : 0,//0:没有操作，1:顶,2:踩；,
						laugh: item.ding_count, //笑哭
						cry: item.cai_count,
					},
					publish: item.comment_count, //发布
					forward: item.sharenum //转发
				}
			},
			ontabtap(index) {
				this.tabIndex = index;
			},
			//滑动改变
			tabchange(e){
				// console.log(e)
				this.tabIndex=e.detail.current;
				thiss.getList()
			},
			loadMore(index) {
				console.log(index)
				if(this.newsList[index].loadtext!="上拉加载更多"){return}
				//修改状态
				this.newsList[index].loadtext="加载中...";
				setTimeout(()=>{
					//获取完成
					let obj={
						headPortrait: '../../static/img/userpic/1.jpg', //头像
						nickName: '小幸福', //昵称
						isFollow: true, //是否关注,
						title: '知识改变一切', //内容
						titlePic: '../../static/img/datapic/1.jpg',
						type: 'img', //图片或者视频
						info: {
							infoOperater: 1, //0没操作   1笑脸 2哭
							laugh: 20, //笑哭
							cry: 10,
						},
						publish: '20w', //发布
						forward: '122' //转发
					}
					this.newsList[index].list.push(obj);
					this.newsList[index].loadtext="上拉加载更多"
				},1000)
				// this.newsList[index].loadtext="我也是有底线的"
			}
		}
	}
</script>

<style lang="scss">
	
	.scroll-v {
	   height: 100%;
	}
	
	
	
</style>
